package com.example.csetpp.controller;

import com.example.csetpp.pojo.dto.ApiTimesGetDTO;
import com.example.csetpp.pojo.vo.user.UniqueVisitorVO;
import com.example.csetpp.service.impl.ApiUsageCounterService;
import com.example.csetpp.service.impl.UniqueVisitorService;
import com.example.csetpp.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "客户量/接口调用次数统计")
public class VisitorStatsController {
    @Autowired
    private UniqueVisitorService uniqueVisitorService;
    @Autowired
    private ApiUsageCounterService apiUsageCounterService;

    @PostMapping("/stats/uv")
    @Operation(summary = "客户量统计")
    public Result<UniqueVisitorVO>  getUniqueVisitors() {
        return Result.success(uniqueVisitorService.getUV());
    }

    @PostMapping("/stats/used-times")
    @Operation(summary = "接口调用次数统计")
    public Result<UniqueVisitorVO> getUsedTimes(@RequestBody ApiTimesGetDTO apiTimesGetDTO)
    {
        log.info("{}", apiTimesGetDTO);
        return Result.success(apiUsageCounterService.getUV(apiTimesGetDTO));
    }

//    @PostMapping("/stats/uv")
//    public long getUniqueVisitorsForDate(@RequestParam("date") String dateStr) {
//        try {
//            LocalDate date = LocalDate.parse(dateStr); // Expects yyyy-MM-dd
//            return uniqueVisitorService.getUniqueVisitorCountForDate(date);
//        } catch (DateTimeParseException e) {
//            // Handle error: invalid date format
//            return -1; // Or throw an exception
//        }
//    }
}
